package com.heima.admin.controller.v1;

import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.admin.dtos.ChannelDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "频道管理",description = "频道管理API")
public class ChannelController {

    @Autowired
    private AdChannelService channelService;
    /**
     * 分页查询频道数据
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据名称分页查询频道列表")
    @ApiImplicitParam(name = "dto", value = "频道请求对象", required = true)
    @PostMapping("/api/v1/channel/list")
    public PageResult<ChannelDto> findByPage(@RequestBody ChannelDto dto){
        PageResult<ChannelDto> pageResult = channelService.findByPage(dto);
        return pageResult;
    }

    /**
     * 保存
     * @param dto
     * @return
     */
    @ApiOperation(value = "频道新增")
    @ApiImplicitParam(name = "dto", value = "频道请求对象", required = true)
    @PostMapping("/api/v1/channel/save")
    public ResponseResult channelSave(@RequestBody ChannelDto dto){
        channelService.channelSave(dto);
        return ResponseResult.ok();
    }

    /**
     * 修改
     * @param dto
     * @return
     */
    @ApiOperation(value = "频道修改")
    @ApiImplicitParam(name = "dto", value = "频道请求对象", required = true)
    @PutMapping("/api/v1/channel/update")
    public ResponseResult channelUpdate(@RequestBody ChannelDto dto){
        channelService.channelUpdate(dto);
        return ResponseResult.ok();
    }

    /**
     * 删除
     * @param channelId
     * @return
     */
    @ApiOperation(value = "频道删除")
    @DeleteMapping("/api/v1/channel/del/{id}")
    public  ResponseResult channelDelete(@PathVariable(name = "id")Integer channelId){
        channelService.channelDelete(channelId);
        return ResponseResult.ok();
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/api/v1/channel/channels")
    public ResponseResult<List<AdChannel>> channels(){
        return ResponseResult.ok(channelService.list());
    }
}
